/**
	*****************************************************************************
	* @file     cmem7_i2c.h
	*
	* @brief    CMEM7 I2C header file
	*
	*
	* @version  V1.0
	* @date     3. September 2013
	*
	* @note               
	*           
	*****************************************************************************
	* @attention
	*
	* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
	* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
	* TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 
	* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
	* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
	* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
	*
	* <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
	*****************************************************************************
	*/
	
#ifndef __CMEM7_I2C_H
#define __CMEM7_I2C_H

#ifdef __cplusplus
 extern "C" {
#endif

#include "cmem7.h"
#include "cmem7_conf.h"


#define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C0) || \
                                   ((PERIPH) == I2C1))


#define I2C_Mode_Slave                  0
#define I2C_Mode_Master                 1
#define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_Slave) || \
                           ((MODE) == I2C_Mode_Master))

#define I2C_ADDR_WIDTH_7BIT                  0
#define I2C_ADDR_WIDTH_10BIT                 1
#define IS_I2C_ADDR_WIDTH(WIDTH) (((WIDTH) == I2C_ADDR_WIDTH_7BIT) || \
                                  ((WIDTH) == I2C_ADDR_WIDTH_10BIT))
			
#define I2C_INT_RX_FIFO_UNDERFLOW      0x00000001    
#define I2C_INT_RX_FIFO_OVERFLOW       0x00000002
#define I2C_INT_RX_FIFO_FULL           0x00000004
#define I2C_INT_TX_FIFO_OVERFLOW       0x00000008    
#define I2C_INT_TX_FIFO_EMPTY          0x00000010
#define I2C_INT_RD_REQUEST             0x00000020
#define I2C_INT_TX_ABORT               0x00000040
#define I2C_INT_RX_DONE                0x00000080
#define I2C_INT_TX_DONE                0x00000100
#define I2C_INT_WR_REQUEST             0x00000200
#define I2C_INT_ALL                    0x000003FF

#define IS_I2C_INT(INT)                (((INT) & ~I2C_INT_ALL) == 0)


typedef struct
{
  uint32_t I2C_Freq;                /*!< I2C frquency */

	uint16_t I2C_TsuDat;              /*!< nano second of TSU:DAT */
  uint16_t I2C_Tsetup;              /*!< nano second of THD:STA and TSU:STO */ 
  uint16_t I2C_Tbuf;                /*!< nano second of TBUF */       	
	uint16_t I2C_TsuSta;              /*!< nano second of TSU:STA */ 
	
	BOOL I2C_SdaFilterEn;             /*!< enabled flag of SDA filter */
	uint8_t I2C_SdaFilterSpike;       /*!< spikes of SDA filter */
	BOOL I2C_SclFilterEn;             /*!< enabled flag of SCL filter */
	uint8_t I2C_SclFilterSpike;       /*!< spikes of SCL filter */
	
}I2C_InitTimingDef;

typedef struct
{
  uint8_t I2C_Mode;                 /*!< Specifies the I2C mode.
                                         This parameter can be a value of @ref I2C_mode */
	uint8_t I2C_AddressWidth;         /*!< 7- or 10-bits width address */
	BOOL I2C_StartByte;               /*!< If start byte exists as a master */
	uint8_t I2C_Address;              /*!< 7- or 10-bits address */

	BOOL I2C_SlaveNack;               /*!< send a NACK after receiving each byte */
 
  I2C_InitTimingDef* timing;        /*!< timing structure, null if don't set */	
	
}I2C_InitTypeDef;

void I2C_Init(I2C0_Type* I2Cx, I2C_InitTypeDef* I2C_Init);
void I2C_Enable(I2C0_Type* I2Cx, BOOL enable);
void I2C_EnableInt(I2C0_Type* I2Cx, uint32_t Int, BOOL enable);
BOOL I2C_GetIntStatus(I2C0_Type* I2Cx, uint32_t Int);
void I2C_ClearInt(I2C0_Type* I2Cx, uint32_t Int);

BOOL I2C_MasterAckPolling(I2C0_Type* I2Cx);
BOOL I2C_MasterReadReq(I2C0_Type* I2Cx, uint8_t addr, uint8_t size);
/* return value is actual read data size */
uint8_t I2C_ReadFifo(I2C0_Type* I2Cx, uint8_t size, uint8_t* data);

/* The first data will be written with request */
BOOL I2C_WriteReq(I2C0_Type* I2Cx, uint8_t size, uint8_t firstData);
/* return value is actual read data size */
uint8_t I2C_WriteFifo(I2C0_Type* I2Cx, uint8_t size, uint8_t* data);


#ifdef __cplusplus
}
#endif

#endif /*__CMEM7_I2C_H */

